import java.util.Stack;

public class Sort {



    /*//1插入排序
    时间复杂度
    最好情况o（n）
    最坏o（n^2）
    空间复杂度
    稳定性：稳定
    一个本身就稳定的排序，可以变成不稳定的
    一个本身不稳定的，不可能变成稳定的
    */
    public static void insertSort(int[] array){
        for (int i = 1; i < array.length; i++) {
            int temp=array[i];
          int j=i-1;
            for (; j>=0; j--) {
                if (array[j]>temp){
                    array[j+1]=array[j];
                }else {
                    break;
                }
                array[j]=temp;
            }

        }
    }
    //2希尔排序
    public static void shellSort(int[] array){
        int gap=array.length;
        while (gap>1){
            gap/=2;
            shell(array,gap);
        }
    }
    public static void shell(int[] array,int gap){
        for (int i = 1; i < array.length; i++) {
            int temp=array[i];
            int j=i-gap;
            for (; j>=0; j-=gap) {
                if (array[j]>temp){
                    array[j+gap]=array[j];
                }else {
                    break;
                }
                array[j]=temp;
            }

        }
    }
    //选择排序
    public static void selectSort(int[] array){

        for (int i = 0; i < array.length; i++) {
            int minIndex=i;
            for (int j =i+1; j <array.length ; j++) {
                if (array[j]<array[minIndex]){
                   minIndex=j;
                }
            }
           swap(array,i,minIndex);

        }
    }

    public static void swap(int[] array,int i,int j){
        int temp=array[i];
        array[i]=array[j];
        array[j]=temp;
    }
    //2定义两个下标
    public static void selectSort2(int[] array){
int left=0;
int right=array.length-1;
while (left<right) {
    int minIndex=left;
    int maxIndex=left;
    for (int i = left + 1; i < right; i++) {
        if (array[i]<array[minIndex]){
           minIndex=i;
        }
        if (array[i]>array[maxIndex]){
           maxIndex=i;
        }

    }
    swap(array,left,minIndex);
    if (maxIndex==left){
        maxIndex=minIndex;
    }
    swap(array,right,maxIndex);
    left++;
    right--;
}
    }

//堆排序
    public static void heapSort(int[] array){
        createBigHeap(array);
        int end=array.length-1;
        while (end>0){
            swap(array,0,end);
            shiftDown(array,0,end);
            end--;
        }
    }
    public static void createBigHeap(int[] array){
        int parent=(array.length-1-1)/2;
        int end=array.length;
        while (parent>0){
            shiftDown(array,parent,end);
            parent--;
        }
    }
public static void shiftDown(int[] array,int parent,int end){
        int child=2*parent+1;
        while (child<end){
            if (child+1<end && array[child]<array[child+1]){
                child++;
            }
            if (array[child]>array[parent]){
                swap(array,parent,child);
                parent=child;
                child=2*parent+1;
            }else {
                break;
            }
        }
}
//冒泡排序
    public static void bubbleSort(int[] array){
        for (int i = 0; i < array.length-1; i++) {
            for (int j = 0; j < array.length-1-i; j++) {
                if (array[j]>array[j+1]){
                    swap(array,j,j+1);
                }
            }
        }
    }
    //快速排序
    public static void QuickSort(int[] array){
        quick(array,0,array.length-1);
    }
    public static void quick(int[] array,int start,int end){
        if (start>=end)
            return;
        int pivot=partition(array,start,end);
        quick(array,start,pivot-1);
        quick(array,pivot+1,end);
    }
    //霍尔法
    public static int partition(int[] array,int left,int right){

      int key=array[left];
      int i=left;

            while (left<right&&array[right]>=key){
                right--;
            }//一定right先走
        while (left<right){
            while (left<right&&array[left]<=key){
                left++;
            }
            swap(array,left,right);
        }
        swap(array,left,i);
        return left;
    }
    //挖坑法
    public static int partition2(int[] array,int left,int right){

        int key=array[left];

        while (left<right&&array[right]>=key){
            right--;
        }//一定right先走
        array[left]=array[right];
        while (left<right){
            while (left<right&&array[left]<=key){
                left++;
            }
           array[right]=array[left];
        }
      array[left]=key;
        return left;
    }

    //双指针法

    public static int partition3(int[] array,int left,int right){
        int prev=left;
        int cur=left+1;
        while (cur<=right){
            if(array[cur]<array[left]&&array[++prev]!=array[cur]){
                swap(array,cur,prev);
            }
        }
        swap(array,prev,left);
        return prev;
    }
//快速排序的非递归
    public static void  QuickSortNor(int[] array){
        int left=0;
        int right=array.length-1;
        Stack<Integer> stack=new Stack<>();
        int pivot=partition(array,left,right);
        if (pivot-1>left){
            stack.push(left);
            stack.push(pivot-1);
        }
        if (pivot+1<right){
            stack.push(pivot+1);
            stack.push(right);
        }
        while (!stack.empty()){
            right=stack.pop();
            left=stack.pop();
            pivot=partition(array,left,right);
            if (pivot-1>left){
                stack.push(left);
                stack.push(pivot-1);
            }
            if (pivot+1<right){
                stack.push(pivot+1);
                stack.push(right);
            }
        }
    }
//归并排序
    public static void mergeSort(int[] array){
        int start=0;
        int end=array.length-1;
        merSortFunc(array,start,end);
    }

    private static void merSortFunc(int[] array, int left, int right) {
       if (left>=right)
           return;
        int mid=(left+right)/2;
        merSortFunc(array,left,mid);
        merSortFunc(array,mid+1,right);
        merge(array,left,right,mid);
    }

    private static void merge(int[] array, int left, int right, int mid) {
        int s1=left;
        int s2=mid+1;
        int k=0;
        int[] newArray=new int[right-left+1];
        while (s1<=mid &&s2<=right){
            if (array[s1]<array[s2]){
                newArray[k++]=array[s1++];
            }else {
                newArray[k++]=array[s2++];
            }
        }
        while (s1<=mid){
            newArray[k++]=array[s1++];
        }
        while (s2<=right){
            newArray[k++]=array[s2++];
        }
        for (int i = 0; i < newArray.length; i++) {
            array[i+left]=newArray[i];
        }
    }
    //归并排序的非递归
    public static void mergeSortNor(int[] array){
        int gap=1;
        while (gap<array.length){
            for (int i = 0; i < array.length; i+=2*gap) {
                int left=i;
                int mid=left+gap-1;
                int right=mid+gap;
                if (mid>=array.length){
                    mid=array.length-1;
                }
                if (right>=array.length){
                    right=array.length-1;
                }
                merge(array,left,mid,right);
            }
            gap*=2;
        }
    }
//奇数排序，主要是针对有很多一样的数
    public static void countSort(int[] array){
        int min=array[0];
        int max=array[0];
        for (int i = 0; i < array.length; i++) {
            if (array[i]<min){
                min=array[i];
            }
        }
        for (int i = 0; i < array.length; i++) {
            if (array[i]>max){
                max=array[i];
            }
            int[] count=new int[max-min+1];
            for (int j = 0; j < array.length; j++) {
                count[array[i]-min]++;
            }
             int index=0;
            for (int j = 0; j < count.length; j++) {
                while (count[i]>0){
                    array[index]=i+min;
                    index++;
                    count[i]--;
            }
            }
        }
    }
}
